Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response: parse: Include input JSON in Error, on JSON parsing errors #32

Merged
merged 5 commits into from
Oct 16, 2024

Conversation

abetomo
Copy link
Contributor

@abetomo abetomo commented Oct 15, 2024

For example, when the input is ["header", :{"return_code":-77}}, a parse error.
The error is then as follows.

`parse': unexpected token at ':{"return_code":-77}}' (JSON::ParserError)

Include the input JSON in the error since the error does not include the full input JSON, making it difficult to debug.

For example, when the input is `["header", :{"return_code":-77}}`, a parse error.
The error is then as follows.
```
`parse': unexpected token at ':{"return_code":-77}}' (JSON::ParserError)
```

Include the input JSON in the error since the error does not include the full input JSON, making it difficult to debug.
Comment on lines 79 to 84
if callback and
/\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
response = JSON.parse($1)
response = json_parse.call($1)
else
response = JSON.parse(raw_response)
response = json_parse.call(raw_response)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception#exceptionなんて初めて知ったよ!でも、ここでlambdaを使う必要はないんじゃないかな。$1raw_responseを変数に入れるだけで十分じゃない?

こんなんはどうかな?

diff --git a/lib/groonga/client/request/error.rb b/lib/groonga/client/request/error.rb
index e7ffb16..4bb344b 100644
--- a/lib/groonga/client/request/error.rb
+++ b/lib/groonga/client/request/error.rb
@@ -36,6 +36,21 @@ module Groonga
           super(message)
         end
       end
+
+      class InvalidResponse < Error
+        attr_reader :command
+        attr_reader :raw_response
+        def initialize(command, raw_response, error_message)
+          @command = commadn
+          @raw_response = raw_response
+          message = +"invalid response: "
+          message << "#{command.command_name}: "
+          message << "#{error_message}: "
+          message << command.to_command_format
+          message << ": <#{raw_response}>"
+          super(message)
+        end
+      end
     end
   end
 end
diff --git a/lib/groonga/client/response/base.rb b/lib/groonga/client/response/base.rb
index 2bb8f19..72154cd 100644
--- a/lib/groonga/client/response/base.rb
+++ b/lib/groonga/client/response/base.rb
@@ -73,9 +73,16 @@ module Groonga
               callback = command["callback"]
               if callback and
                   /\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
-                response = JSON.parse($1)
+                json = JSON.parse($1)
               else
-                response = JSON.parse(raw_response)
+                json = JSON.parse(raw_response)
+              end
+              begin
+                response = JSON.parse(json)
+              rescue JSON::ParseError => error
+                raise InvalidResponse.new(command,
+                                          raw_response,
+                                          "invalid JSON: #{error}")
               end
               if response.is_a?(::Array)
                 header, body = response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

差分が極小になる方向で頑張りましたが、 InvalidResponse を追加する感じに変更しました!

Copy link

@otegami otegami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏾

@@ -66,6 +67,21 @@ def line
end
end
end

class InvalidResponse < Client::Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このファイルって例外的なレスポンス用のファイルじゃなくて、想定内のエラーレスポンス用のファイルだから、例外的なことをここのファイルには入れないほうがいいかな。

Groonga::Client::Responseの下に置くのはやり過ぎな感じがするし、lib/groonga/client/error.rbGroonga::Clientの下でいいかな。

そうすると、lib/groonga/client/request/error.rblib/groonga/client/error.rbに移動して、Groonga::Client直下にしたくなるね。

diff --git a/lib/groonga/client/error.rb b/lib/groonga/client/error.rb
index 6ec96d8..43fd601 100644
--- a/lib/groonga/client/error.rb
+++ b/lib/groonga/client/error.rb
@@ -18,5 +18,20 @@ module Groonga
   class Client
     class Error < StandardError
     end
+
+    class ErrorResponse < Error
+      attr_reader :response
+      def initialize(response)
+        @response = response
+        command = @response.command
+        status_code = @response.status_code
+        error_message = @response.error_message
+        message = "failed to execute: "
+        message << "#{command.command_name}: #{status_code}: "
+        message << "<#{error_message}>: "
+        message << command.to_command_format
+        super(message)
+      end
+    end
   end
 end
diff --git a/lib/groonga/client/request/error.rb b/lib/groonga/client/request/error.rb
index e7ffb16..e7678ed 100644
--- a/lib/groonga/client/request/error.rb
+++ b/lib/groonga/client/request/error.rb
@@ -19,23 +19,9 @@ require "groonga/client/error"
 module Groonga
   class Client
     module Request
-      class Error < Client::Error
-      end
-
-      class ErrorResponse < Error
-        attr_reader :response
-        def initialize(response)
-          @response = response
-          command = @response.command
-          status_code = @response.status_code
-          error_message = @response.error_message
-          message = "failed to execute: "
-          message << "#{command.command_name}: #{status_code}: "
-          message << "<#{error_message}>: "
-          message << command.to_command_format
-          super(message)
-        end
-      end
+      # For backward compatibility
+      Error = Client::Error
+      ErrorResponse = Client::ErrorResponse
     end
   end
 end

Copy link
Contributor Author

@abetomo abetomo Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ナルホド。そっちのエラーだったのか。

Comment on lines 80 to 81
message << command.to_command_format
message << ": <#{raw_response}>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実際のメッセージを見ると読みにくいからちょっと印を入れようか。

Suggested change
message << command.to_command_format
message << ": <#{raw_response}>"
message << "<#{command.to_command_format}>: "
message << "<#{raw_response}>"

Comment on lines 229 to 237
err = assert_raise(Groonga::Client::Response::InvalidResponse) do
Groonga::Client::Response::Base.parse(command, raw_response)
end
error_message = <<~'MESSAGE'.chomp
invalid response: cancel: invalid JSON: unexpected token at ':{"return_code":-77}}': cancel: <["header", :{"return_code":-77}}>
MESSAGE
assert_equal(error_message, err.message)
assert_equal(command, err.command)
assert_equal(raw_response, err.raw_response)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでいける?

Suggested change
err = assert_raise(Groonga::Client::Response::InvalidResponse) do
Groonga::Client::Response::Base.parse(command, raw_response)
end
error_message = <<~'MESSAGE'.chomp
invalid response: cancel: invalid JSON: unexpected token at ':{"return_code":-77}}': cancel: <["header", :{"return_code":-77}}>
MESSAGE
assert_equal(error_message, err.message)
assert_equal(command, err.command)
assert_equal(raw_response, err.raw_response)
begin
JSON.parse(raw_response)
rescue JSON::ParseError => error
parse_error_message = error.to_s
end
error = Groonga::Client::InvalidResponse.new(command, raw_response, parse_error_message)
assert_raise(error) do
Groonga::Client::Response::Base.parse(command, raw_response)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いけました。

lib/groonga/client/error.rb Outdated Show resolved Hide resolved
@kou kou merged commit 25f4b4a into ranguba:master Oct 16, 2024
4 checks passed
@abetomo abetomo deleted the input-json-is-included-in-error branch October 16, 2024 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants